//
//  UnitViewController.m
//  Estate
//
//  Created by heyong on 16/6/22.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "UnitViewController.h"
#import "DefaineCell.h"
#import "TitleAndContentCell.h"

@interface UnitViewController ()
@property (strong, nonatomic)NSMutableArray *titleForSec1;
@property (strong, nonatomic)FMDatabase *db;
@property (strong, nonatomic)NSMutableArray *dataArr;
@end

@implementation UnitViewController
- (FMDatabase *)db{
    if(_db==nil){
        _db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    }
    return  _db;
}

- (NSMutableArray *)titleForSec1{
    if(_titleForSec1==nil){
        _titleForSec1=[NSMutableArray arrayWithObjects:@"单位名称:",@"联系电话:" ,@"单位地址:" ,@"邮编:",@"领导:",@"工程部:",nil];
    }
    return _titleForSec1;
}

-(NSMutableArray *)dataArr{
    if(_dataArr==nil){
        _dataArr=[NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.bounces=NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"deCell"];
    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    [self initData];
}

- (void)initData{
    
    NSString *sqlStr=[NSString stringWithFormat:@"SELECT ID,PRO_UNIT_NAME,PRO_UNIT_PHONE,PRO_UNIT_ADDRESS,PRO_UNIT_EMAIL FROM PROJECT_UNIT WHERE PRO_ID='%@' and PRO_UNIT_TYPE='UNIT_01'",PROID];
    [self.db open];
    FMResultSet *result=[self.db executeQuery:sqlStr];
    while ([result next]) {
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setObject:[result stringForColumn:@"PRO_UNIT_NAME"] forKey:@"name"];
        [dic setObject:[result stringForColumn:@"PRO_UNIT_PHONE"] forKey:@"phone"];
        [dic setObject:[result stringForColumn:@"PRO_UNIT_ADDRESS"] forKey:@"address"];
        [dic setObject:[result stringForColumn:@"PRO_UNIT_EMAIL"] forKey:@"email"];
        
        //查询领导和工程部
        NSString *unitId=[result stringForColumn:@"ID"];
        
        NSString *sqlStr2=[NSString stringWithFormat:@"SELECT USER_ID,USER_TYPE FROM PROJECT_USER WHERE UNIT_ID='%@'",unitId];
        FMResultSet *result2=[self.db executeQuery:sqlStr2];
        
        //领导数组
        NSMutableArray *leaderArray=[NSMutableArray array];
        //工程部数组
        NSMutableArray *gcbArray=[NSMutableArray array];
        
        while ([result2 next]) {
            //领导
            if([[result2 stringForColumn:@"USER_TYPE"]isEqualToString:@"BUILD_01"]){
                //得到userid
                NSString *userId=[result2 stringForColumn:@"USER_ID"];
                
                [leaderArray addObject:DEFISNUL([LSUtility getuserIdByUserName:userId])[@"REAL_NAME"] ];
                
            }
            //工程部
            if([[result2 stringForColumn:@"USER_TYPE"]isEqualToString:@"BUILD_02"]){
                NSString *userId=[result2 stringForColumn:@"USER_ID"];
                [gcbArray addObject:DEFISNUL([LSUtility getuserIdByUserName:userId])[@"REAL_NAME"] ];
            }
        }
        
        if(leaderArray.count>0){
            //添加领导
            [dic setObject:leaderArray forKey:@"lingdao"];
        }else{
            [leaderArray addObject:@""];
            [dic setObject:leaderArray forKey:@"lingdao"];
            
        }
        if(gcbArray.count>0){
            //添加工程部
            [dic setObject:gcbArray forKey:@"gongcheng"];
        }else{
            [gcbArray addObject:@""];
            [dic setObject:gcbArray forKey:@"gongcheng"];
        }
        
        
        [self.dataArr addObject:dic];
    }
    [self.db close];
}


#pragma mark -UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic=self.dataArr[indexPath.section];
    long row=indexPath.row;
    if(row==0){
        return [TitleAndContentCell cellContentHeight:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],dic[@"name"]]];
        
    }else if(row==1){
        return [TitleAndContentCell cellContentHeight:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],dic[@"phone"]]];
        
    }else if (row==2){
        return [TitleAndContentCell cellContentHeight:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],dic[@"address"]]];
        
        
    }else if (row==3){
        return [TitleAndContentCell cellContentHeight:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],dic[@"email"]]];
        
        
    }else if (row==4){
        NSArray *ld=dic[@"lingdao"];
        NSString *text=@"";
        for (NSString *str in ld) {
            text=[text stringByAppendingString:[NSString stringWithFormat:@"%@  ",str]];
        }
        return [TitleAndContentCell cellContentHeight:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],text]];
    }else{
        
        NSArray *ld=dic[@"gongcheng"];
        NSString *text=@"";
        for (NSString *str in ld) {
            text=[text stringByAppendingString:[NSString stringWithFormat:@"%@  ",str]];
        }
        return [TitleAndContentCell cellContentHeight:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],text]];
    }
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 8)];
    img.backgroundColor=[UIColor whiteColor];
    img.image=[UIImage imageNamed:@"juchishang"];
    return img;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 0;
    }
    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic=self.dataArr[indexPath.section];
    long row=indexPath.row;
    //    DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
    //    cell.title.text=self.titleForSec1[row];
    
    TitleAndContentCell *cell=[TitleAndContentCell cellWithTableView:tableView];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    [cell setTitleContent:self.titleForSec1[row]];
    
    
    if(row==0){
        [cell setTitleContent:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],DEFISNUL(dic[@"name"])]];
        
    }else if (row==1){
        [cell setTitleContent:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],DEFISNUL(dic[@"phone"])]];
        
    }else if(row==2){
        [cell setTitleContent:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],DEFISNUL(dic[@"address"])]];
        
    }else if(row==3){
        [cell setTitleContent:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],DEFISNUL(dic[@"email"])]];
        
    }else if(row==4){
        NSArray *ld=dic[@"lingdao"];
        NSString *text1=@"";
        for (NSString *str in ld) {
            text1=[text1 stringByAppendingString:[NSString stringWithFormat:@"%@  ",str]];
        }
        
        [cell setTitleContent:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],DEFISNUL(text1)]];
        
    }else if(row==5){
        
        NSArray *gc=dic[@"gongcheng"];
        NSString *str=@"";
        for (NSString *s in gc) {
            str=[str stringByAppendingString:[NSString stringWithFormat:@"%@  ",s]];
        }
        DeLog(@"text2:%@",str);
        [cell setTitleContent:[NSString stringWithFormat:@"%@ %@",self.titleForSec1[row],str]];
        
    }
    return cell;
}

/*
 *  此方法让cell横线置顶
 */
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
